package com.storage.system.service.impl;

import java.util.List;

import com.storage.common.core.domain.AjaxResult;
import com.storage.common.enums.OwnerType;
import com.storage.common.enums.UserAndAgentLevelType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.storage.system.mapper.StorageUserAgentMapper;
import com.storage.system.domain.StorageUserAgent;
import com.storage.system.service.IStorageUserAgentService;
import com.storage.common.core.text.Convert;

/**
 * 用户-代理商 服务层实现
 * 
 * @author storage
 * @date 2019-08-18
 */
@Service
public class StorageUserAgentServiceImpl implements IStorageUserAgentService 
{
	@Autowired
	private StorageUserAgentMapper storageUserAgentMapper;

	/**
     * 查询用户-代理商信息
     * 
     * @param id 用户-代理商ID
     * @return 用户-代理商信息
     */
    @Override
	public StorageUserAgent selectStorageUserAgentById(Long id)
	{
	    return storageUserAgentMapper.selectStorageUserAgentById(id);
	}
	
	/**
     * 查询用户-代理商列表
     * 
     * @param storageUserAgent 用户-代理商信息
     * @return 用户-代理商集合
     */
	@Override
	public List<StorageUserAgent> selectStorageUserAgentList(StorageUserAgent storageUserAgent)
	{
	    return storageUserAgentMapper.selectStorageUserAgentList(storageUserAgent);
	}
	
    /**
     * 新增用户-代理商
     * 
     * @param storageUserAgent 用户-代理商信息
     * @return 结果
     */
	@Override
	public int insertStorageUserAgent(StorageUserAgent storageUserAgent)
	{
	    return storageUserAgentMapper.insertStorageUserAgent(storageUserAgent);
	}
	
	/**
     * 修改用户-代理商
     * 
     * @param storageUserAgent 用户-代理商信息
     * @return 结果
     */
	@Override
	public int updateStorageUserAgent(StorageUserAgent storageUserAgent)
	{
	    return storageUserAgentMapper.updateStorageUserAgent(storageUserAgent);
	}

	/**
     * 删除用户-代理商对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteStorageUserAgentByIds(String ids)
	{
		return storageUserAgentMapper.deleteStorageUserAgentByIds(Convert.toStrArray(ids));
	}

	/**
	 * 添加用户与代理商的关联关系
	 *
	 * @param userId	用户id
	 * @param agentId	代理商id
	 * @param userLevel	用户与代理商关联关系(1-当前用户是代理商的下级,2-当前用户是代理商的上级)
	 * @return
	 */
    @Override
    public AjaxResult addUserAndAgentRelationship(Long userId, Long agentId, int userLevel, String acceptBrand) {

    	if (userLevel == UserAndAgentLevelType.DOWN.getValue()) {
			// 被添加用户为当前用户的下级
			StorageUserAgent user = new StorageUserAgent(userId, agentId, UserAndAgentLevelType.DOWN.getValue(), OwnerType.AGENT.name(), acceptBrand);
			StorageUserAgent agent = new StorageUserAgent(agentId, userId, UserAndAgentLevelType.UP.getValue(), OwnerType.AGENT.name(), null);
			storageUserAgentMapper.insertStorageUserAgent(user);
			storageUserAgentMapper.insertStorageUserAgent(agent);
		} else if(userLevel == UserAndAgentLevelType.UP.getValue()) {
			// 被添加用户为当前用户的上级
			StorageUserAgent user = new StorageUserAgent(userId, agentId, UserAndAgentLevelType.UP.getValue(), OwnerType.AGENT.name(), acceptBrand);
			StorageUserAgent agent = new StorageUserAgent(agentId, userId, UserAndAgentLevelType.DOWN.getValue(), OwnerType.AGENT.name(), null);
			storageUserAgentMapper.insertStorageUserAgent(user);
			storageUserAgentMapper.insertStorageUserAgent(agent);
		}

        return AjaxResult.success("关联关系添加成功");
    }

}
